﻿using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;

namespace 使用未编译的XAML和代码创建wpf应用程序 {

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        private Button Button1;
        public MainWindow() {
            InitializeComponent();
        }

        public MainWindow(string xmalFileName) {
            //设置窗体
            this.Width = this.Height = 500;
            this.Left = this.Top = 1000;
            this.Title = "Dynamically Loaded XAML";

            //从外部文件获取xaml文档内容
            DependencyObject rootElement;
            using (FileStream fs = new FileStream(xmalFileName, FileMode.Open)) {
                rootElement =(DependencyObject) XamlReader.Load(fs);
            }

            this.Content = rootElement;

            Button1 = LogicalTreeHelper.FindLogicalNode(rootElement, "Button1") as Button;
            Button1.Click += Button1_Click;
        }

        private void Button1_Click(object sender, RoutedEventArgs e) {
            MessageBox.Show("Clicked");
        }
    }
}
